package com.example.myapplication.part12;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewActivity extends AppCompatActivity {

    private List<Flower> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view); // 确保布局文件正确
        initFruits();

        // 直接使用 findViewById 返回的 RecyclerView 类型
        RecyclerView recyclerView = findViewById(R.id.report_drawn);
        StaggeredGridLayoutManager layoutManager =
                new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        FlowerAdapter adapter = new FlowerAdapter(fruitList);
        recyclerView.setAdapter(adapter);
    }

    private void initFruits() {
        for (int i = 0; i < 3; i++) {
            Flower flower1 = new Flower(getRandomLengthName("fl"), R.drawable.f1_pic);
            fruitList.add(flower1);
            Flower flower2 = new Flower(getRandomLengthName("f2"), R.drawable.f2_pic);
            fruitList.add(flower2);
            Flower flower3 = new Flower(getRandomLengthName("f3"), R.drawable.f3_pic);
            fruitList.add(flower3);
            Flower flower4 = new Flower(getRandomLengthName("f4"), R.drawable.f4_pic);
            fruitList.add(flower4);
        }
    }

    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(name);
        }
        return builder.toString();
    }
}
